/**
 * Created by sunkejun on 2015/10/15.
 */

$(document).ready(function(){
    //console.log(  $('#dataTable'));
    var table = new ComDataTable("/admin/post/page",$('#dataTable'),"admin_post");
    var renders = {
        id:admin_post_list.editRender,
        tag_names:admin_post_list.tags_render,
        use_seo:admin_post_list.seo_render,
        post_status:admin_post_list.status_render,
    };
    table.setRenders(renders);
    table.init();
    admin_post_list.$table=table;
    //<span class="label label-primary">Enable</span>
})
var admin_post_list  = (function ($) {
    this.editRender = function( data, type, full, meta ) {

        return '<a class="btn btn-sm btn-warning"  tooltip="修改"  href="/admin/post/create/?id='+data+'"  >  <span class="fa fa-edit"></span>  </a>'+
            '&nbsp;&nbsp<a class="btn btn-sm btn-danger"  tooltip="删除"   onclick="admin_post_list.delete('+data+')">  <span class="fa fa-trash"></span></a>';
    }
    this.tags_render = function( data, type, full, meta ) {

        if(data == null || data == ""){
            return "";
        }
        var tags = data.split(',');
        //console.log(str);
        var str = "";
        for(var i=0; i<tags.length;i++){
            str+='<span class="label label-default" style="margin-left:5px;">'+tags[i]+'</span>';
        }
        return str;
    }
    this.status_render = function( data, type, full, meta ) {

        if(data == 1){
           return  '<span class="label label-primary">'+language.getNorlanguage().public+'</span>';
        }else{
            return  '<span class="label label-warning">'+language.getNorlanguage().draft+'</span>';
        }
    }
    this.seo_render = function( data, type, full, meta ) {

        if(data == 1){
           return  '<span class="label label-primary">'+language.getNorlanguage().yes+'</span>';
        }else{
            return  '<span class="label label-warning">'+language.getNorlanguage().no+'</span>';
        }
    }


    this.delete = function(id){
        swal({
            title: language.getNorlanguage().delete_title,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: language.getNorlanguage().delete_sub,
            cancelButtonText: language.getNorlanguage().delete_cancle,
            closeOnConfirm: true,
            html: false
        }, function(){
            ComdataUtil.ajaxpostnormal('/admin/post/'+id,{},function(data){
                this.$table.rdraw();
            },"DELETE");
        });

        //ComdataUtil.ajaxpostnormal('/admin/message/'+id,{},function(data){
        //
        //},"DELETE");
    }

    return this;
})(jQuery);

